clear all
set more off
tempfile ubigeo temp

/*

This dofile randomly allocates exposure to violence after reshufling the age distribution within the sample. 
This ensures we keep the same cohort composition and only change the exposure profile of every individual based on a random age allocation.

In this dofile we work violence from the terrorists

*/

** PATHS
local working ../working

** DATA
use `working'/200, clear
gen x = _n
keep x age
compress
save `temp', replace

forvalues p = 1/200 {

	use `working'/200, clear
	drop age

	gen rank = runiform()
	sort rank
	gen x = _n

	merge 1:1 x using `temp'
	drop _merge

	merge 1:1 conglome vivienda hogar codperso year using `working'/getsample
	keep if _merge == 3
	drop _merge


	local n = 0
	forvalues y = 0/30 {
		gen yearviolence = year - age + `n'
		replace yearviolence = . if `n' > age
		
		merge m:1 ubigeonac1993 yearviolence using `working'/violenceT
		drop if _merge == 2
		drop _merge
		
		rename events eventsTri`y'_`p'
		rename events_fatal events_fatalA`y'
		rename events_nonfatal events_nonfatalA`y'
		drop events_fatalA`y'
		drop events_nonfatalA`y'
		
		*rename terrorists terrorists`y'
		*rename government government`y'
		
		drop yearviolence
		local ++n
	}

	recode events* /*terrorists* government**/ (. = 0)

	keep conglome vivienda hogar codperso year events* //terrorists* government*
	
	recode events* (1/999 = 1)
	
	compress
	save `working'/attachviolenceTri`p', replace
}
